package com.qfedu.mesadmin.sys.user.controller;

import com.qfedu.mes.common.CommonResult;
import com.qfedu.mes.constrants.CommonConstants;
import com.qfedu.mes.entity.sys.SysUser;
import com.qfedu.mesadmin.sys.user.params.LoginParam;
import com.qfedu.mesadmin.sys.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
@RequestMapping("/sys/user")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParam loginParam, HttpSession session) {
        SysUser user = sysUserService.login(loginParam);
        session.setAttribute(CommonConstants.LOGIN_INFO, user);

        System.out.println(session.getId());

        // 配合前端使用，本例的token实际意义
        String token = UUID.randomUUID().toString().replace("-", "");
        return CommonResult.success(token);
    }
}
